package edu.hawaii.myisern.action;


import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;

import static org.junit.Assert.assertEquals;
import org.junit.Test; 


/**
 * Tests operation of the StackStripes Push command.
 *
 * @author Jitender Miglani
 * @author Philip Johnson
 * @author Ben Karsin
 */

public class TestMyIsernActionBean  {
  
  private String testHost = System.getProperty("test.host");
  
  /** The page title. */
  private String loginTitle = "MyISERN - Login";
  
  /**
   * Tests the MyIsernActionBean class.
   * @throws Exception if error occurs.
   */
  @Test
  @SuppressWarnings("deprecation")
  public void testActionBean() throws Exception {
    WebConversation conversation = new WebConversation();

    // Go to login page.
    String Url = testHost + "myisern-fuchsia/login.jsp";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking arrival at login page", loginTitle, response.getTitle());

    // Login to the system.
    WebForm testForm = response.getFormWithID("LoginForm");
    WebRequest testRequest = testForm.getRequest();
    testRequest.setParameter("userName", "admin");
    testRequest.setParameter("password", "changethis");
    response = conversation.getResponse(testRequest);

    // Display Researchers table.
    testForm = response.getFormWithID("PushForm");
    testRequest = testForm.getRequest();
    
    // Test Researchers
    testRequest.setParameter("listToDisplay", "R");
    response = conversation.getResponse(testRequest);
    
    WebTable testTable = response.getTableWithID("dataTable");
    
    assertEquals("Checking number of columns (5)", 5, testTable.getColumnCount());
    assertEquals("Checking 2nd header value", "Organization", 
        testTable.getTableCell(0, 1).asText());
    
//    // Edit last researcher on table.
//    testForm = response.getFormWithID("InnerForm");
//    testRequest = testForm.getRequest();
//    response = conversation.getResponse(testRequest);
//    
//    assertEquals("Checking arrival at Researcher edit page", "MyISERN - Edit Researcher", 
//        response.getTitle());
//    
//    testForm = response.getFormWithID("EditCollabForm");
//    testRequest = testForm.getRequest();
//    
//    assertEquals("Checking name value in box", "Dag Sjoberg", testRequest.getParameter("name"));
//    response = conversation.getResponse(testRequest);
//    
    // Test organizations
    testForm = response.getFormWithID("PushForm");
    testRequest = testForm.getRequest();
    testRequest.setParameter("listToDisplay", "O");
    response = conversation.getResponse(testRequest);
    
    // Check organizations table.
    testTable = response.getTableWithID("dataTable");
    
    assertEquals("Checking number of columns (8)", 8, testTable.getColumnCount());
    assertEquals("Checking 2nd header value", "Type", testTable.getTableCell(0, 1).asText());
    
    // Edit last Organization in table.
//    testForm = response.getFormWithID("InnerForm");
//    testRequest = testForm.getRequest();
//    response = conversation.getResponse(testRequest);
//    
//    assertEquals("Checking arrival at org edit page", "MyISERN - Edit Organization", 
//        response.getTitle());
//    
//    testForm = response.getFormWithID("EditCollabForm");
//    testRequest = testForm.getRequest();
//    
//    assertEquals("Checking name value in box", "National ICT Australia", 
//        testRequest.getParameter("name"));
//    response = conversation.getResponse(testRequest);
//    
    // Test collaborations.
    testForm = response.getFormWithID("PushForm");
    testRequest = testForm.getRequest();
    testRequest.setParameter("listToDisplay", "C");
    response = conversation.getResponse(testRequest);
    
    // Check collaborations table.
    testTable = response.getTableWithID("dataTable");
    
    assertEquals("Checking number of columns (6)", 6, testTable.getColumnCount());
    assertEquals("Checking 4nd header value", "Years", testTable.getTableCell(0, 3).asText());
    
    // Edit last collaboration in table.
    testForm = response.getFormWithID("InnerForm");
    testRequest = testForm.getRequest();
    response = conversation.getResponse(testRequest);
    
    assertEquals("Checking arrival at collab edit page", "MyISERN - Edit Collaboration", 
        response.getTitle());
    
    testForm = response.getFormWithID("EditCollabForm");
    testRequest = testForm.getRequest();
    
//    assertEquals("Checking name value in box", "FIESE-UPM", testRequest.getParameter("name"));
//    response = conversation.getResponse(testRequest);
//    
//    testForm = response.getFormWithID("TableForm");
//    testRequest = testForm.getRequest();
//    response = conversation.getResponse(testRequest);
//
//    assertEquals("Checking arrival at collab edit page", "MyISERN - Add Collaboration", 
//        response.getTitle());
//    
    // Test cancel button for collaborations.
//    testForm = response.getFormWithID("cancel");
//    testRequest = testForm.getRequest();
//    response = conversation.getResponse(testRequest);
    
  }
}
  